//给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 
//
// 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。 
//
// 
//
// 
//
// 示例 1： 
//
// 
//输入：digits = "23"
//输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
// 
//
// 示例 2： 
//
// 
//输入：digits = ""
//输出：[]
// 
//
// 示例 3： 
//
// 
//输入：digits = "2"
//输出：["a","b","c"]
// 
//
// 
//
// 提示： 
//
// 
// 0 <= digits.length <= 4 
// digits[i] 是范围 ['2', '9'] 的一个数字。 
// 
//
// Related Topics 哈希表 字符串 回溯 👍 2463 👎 0

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution17_递归 {
    static HashMap<String, String[]> digitStringMap = new HashMap<>();
    static {
        digitStringMap.put("2", new String[]{"a", "b", "c"});
        digitStringMap.put("3", new String[]{"d", "e", "f"});
        digitStringMap.put("4", new String[]{"g", "h", "i"});
        digitStringMap.put("5", new String[]{"j", "k", "l"});
        digitStringMap.put("6", new String[]{"m", "n", "o"});
        digitStringMap.put("7", new String[]{"p", "q", "r", "s"});
        digitStringMap.put("8", new String[]{"t", "u", "v"});
        digitStringMap.put("9", new String[]{"w", "x", "y", "z"});
    }

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return new ArrayList<>();
        }

        return func(digits, 0);
    }

    private List<String> func(String digits, int begin) {
        String curr = String.valueOf(digits.charAt(begin));
        if (begin == digits.length() - 1) {
            return Arrays.asList(digitStringMap.get(curr));
        }

        String[] firstChars = digitStringMap.get(curr);

        List<String> restList = func(digits, begin + 1);

        ArrayList<String> list = new ArrayList<>();
        for (String first : firstChars) {
            if (restList.isEmpty()) {
                list.add(first);
            } else {
                for (String rest : restList) {
                    list.add(first + rest);
                }
            }
        }

        return list;
    }
}

class Solution17_Loop
{
    static HashMap<String, String[]> digitStringMap = new HashMap<>();
    static {
        digitStringMap.put("2", new String[]{"a", "b", "c"});
        digitStringMap.put("3", new String[]{"d", "e", "f"});
        digitStringMap.put("4", new String[]{"g", "h", "i"});
        digitStringMap.put("5", new String[]{"j", "k", "l"});
        digitStringMap.put("6", new String[]{"m", "n", "o"});
        digitStringMap.put("7", new String[]{"p", "q", "r", "s"});
        digitStringMap.put("8", new String[]{"t", "u", "v"});
        digitStringMap.put("9", new String[]{"w", "x", "y", "z"});
    }

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return new ArrayList<>();
        }

        // 子分支结果
        List<String> restList = new ArrayList<>();

        Stack<Frame> stack = new Stack<>();
        stack.push(new Frame(0, null));
        while (!stack.isEmpty()) {
            Frame frame = stack.pop();
            int begin = frame.begin;
            String curr = String.valueOf(digits.charAt(begin));
            if (begin == digits.length() - 1) {
                restList = Arrays.asList(digitStringMap.get(curr));
                continue;
            }

            if (frame.flag) {
                // 后置处理
                String[] firstChars = digitStringMap.get(curr);

                ArrayList<String> list = new ArrayList<>();
                for (String first : firstChars) {
                    if (restList.isEmpty()) {
                        list.add(first);
                    } else {
                        for (String rest : restList) {
                            list.add(first + rest);
                        }
                    }
                }
                restList = list;
                continue;
            }

            stack.push(frame);
            // List<String> restList = func(digits, begin + 1);
            stack.push(new Frame(begin + 1, null));
            frame.flag = true;
        }

        return restList;
    }


    class Frame {
        int begin;
        List<String> list;
        boolean flag = false;

        public Frame(int begin, List<String> list) {
            this.begin = begin;
            this.list = list;
        }
    }
}

//leetcode submit region end(Prohibit modification and deletion)


